

<!DOCTYPE html>
<!--[if IE]><![endif]-->
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>Don't Call PropTypes Warning - React</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta property="og:title" content="Don't Call PropTypes Warning - React">
  <meta property="og:type" content="website">
  <meta property="og:url" content="https://facebook.github.io/react/warnings/dont-call-proptypes.html">
  <meta property="og:image" content="https://facebook.github.io/react/img/logo_og.png">
  <meta property="og:description" content="A JavaScript library for building user interfaces">
  <meta property="fb:app_id" content="623268441017527">

  <link rel="shortcut icon" href="/favicon.ico">
  <link rel="alternate" type="application/rss+xml" title="React" href="https://facebook.github.io/react/feed.xml">

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.css" />
  <link rel="stylesheet" href="/css/syntax.css">
  <link rel="stylesheet" href="/css/codemirror.css">
  <link rel="stylesheet" href="/css/react.css">

  <script src="//use.typekit.net/vqa1hcx.js"></script>
  <script>try{Typekit.load();}catch(e){}</script>

  <!--[if lte IE 8]>
  <script src="/js/html5shiv.min.js"></script>
  <script src="/js/es5-shim.min.js"></script>
  <script src="/js/es5-sham.min.js"></script>
  <![endif]-->
  <script type="text/javascript" src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"></script>
  <script src="/js/codemirror.js"></script>
  <script src="/js/javascript.js"></script>
  <script src="/js/xml.js"></script>
  <script src="/js/jsx.js"></script>
  <script src="/js/react.js"></script>
  <script src="/js/react-dom.js"></script>
  <script src="/js/babel.min.js"></script>
  <script src="/js/live_editor.js"></script>
</head>
<body>

  <div class="container">

    <div class="nav-main">
  <div class="wrap">
    <a class="nav-home" href="/">
      <img class="nav-logo" src="/img/logo.svg" width="36" height="36">
      React
    </a>
    <div class="nav-lists">
      <ul class="nav-site nav-site-internal">
        <li><a href="/docs/hello-world.html">Docs</a></li>
        <li><a href="/tutorial/tutorial.html">Tutorial</a></li>
        <li><a href="/community/support.html">Community</a></li>
        <li><a href="/blog/">Blog</a></li>
        <li class="nav-site-search">
          <input id="algolia-doc-search" type="text" placeholder="Search docs..." />
        </li>
      </ul>
      <ul class="nav-site nav-site-external">
        <li><a href="https://github.com/facebook/react">GitHub</a></li>
        <li><a href="https://github.com/facebook/react/releases">v16.0.0-alpha.3</a></li>
      </ul>
    </div>
  </div>
</div>


    <section class="content wrap documentationContent nosidebar">
  <div class="inner-content">
    <h1>Don't Call PropTypes Warning</h1>
    <div class="subHeader"></div>
    <p>In a future major release of React, the code that implements PropType validation functions will be stripped in production. Once this happens, any code that calls these functions manually (that isn&#39;t stripped in production) will throw an error.</p>

<h3>Declaring PropTypes is still fine</h3>

<p>The normal usage of PropTypes is still supported:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="nx">Button</span><span class="p">.</span><span class="nx">propTypes</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">highlighted</span><span class="o">:</span> <span class="nx">React</span><span class="p">.</span><span class="nx">PropTypes</span><span class="p">.</span><span class="nx">bool</span>
<span class="p">};</span>
</code></pre></div>
<p>Nothing changes here.</p>

<h3>Don’t call PropTypes directly</h3>

<p>Using PropTypes in any other way than annotating React components with them is no longer supported:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kd">var</span> <span class="nx">apiShape</span> <span class="o">=</span> <span class="nx">React</span><span class="p">.</span><span class="nx">PropTypes</span><span class="p">.</span><span class="nx">shape</span><span class="p">({</span>
  <span class="nx">body</span><span class="o">:</span> <span class="nx">React</span><span class="p">.</span><span class="nx">PropTypes</span><span class="p">.</span><span class="nx">object</span><span class="p">,</span>
  <span class="nx">statusCode</span><span class="o">:</span> <span class="nx">React</span><span class="p">.</span><span class="nx">PropTypes</span><span class="p">.</span><span class="nx">number</span><span class="p">.</span><span class="nx">isRequired</span>
<span class="p">}).</span><span class="nx">isRequired</span><span class="p">;</span>

<span class="c1">// Not supported!</span>
<span class="kd">var</span> <span class="nx">error</span> <span class="o">=</span> <span class="nx">apiShape</span><span class="p">(</span><span class="nx">json</span><span class="p">,</span> <span class="s1">&#39;response&#39;</span><span class="p">);</span>
</code></pre></div>
<p>If you depend on using PropTypes like this, we encourage you to use or create a fork of PropTypes (such as <a href="https://github.com/aackerman/PropTypes">these</a> <a href="https://github.com/developit/proptypes">two</a> packages).</p>

<p>If you don&#39;t fix the warning, this code will crash in production with React 16.</p>

<h3>If you don&#39;t call PropTypes directly but still get the warning</h3>

<p>Inspect the stack trace produced by the warning. You will find the component definition responsible for the PropTypes direct call. Most likely, the issue is due to third-party PropTypes that wrap React’s PropTypes, for example:</p>
<div class="highlight"><pre><code class="language-js" data-lang="js"><span class="nx">Button</span><span class="p">.</span><span class="nx">propTypes</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">highlighted</span><span class="o">:</span> <span class="nx">ThirdPartyPropTypes</span><span class="p">.</span><span class="nx">deprecated</span><span class="p">(</span>
    <span class="nx">React</span><span class="p">.</span><span class="nx">PropTypes</span><span class="p">.</span><span class="nx">bool</span><span class="p">,</span>
    <span class="s1">&#39;Use `active` prop instead&#39;</span>
  <span class="p">)</span>
<span class="p">}</span>
</code></pre></div>
<p>In this case, <code>ThirdPartyPropTypes.deprecated</code> is a wrapper calling <code>React.PropTypes.bool</code>. This pattern by itself is fine, but triggers a false positive because React thinks you are calling PropTypes directly. The next section explains how to fix this problem for a library implementing something like <code>ThirdPartyPropTypes</code>. If it&#39;s not a library you wrote, you can file an issue against it.</p>

<h3>Fixing the false positive in third party PropTypes</h3>

<p>If you are an author of a third party PropTypes library and you let consumers wrap existing React PropTypes, they might start seeing this warning coming from your library. This happens because React doesn&#39;t see a &quot;secret&quot; last argument that <a href="https://github.com/facebook/react/pull/7132">it passes</a> to detect manual PropTypes calls.</p>

<p>Here is how to fix it. We will use <code>deprecated</code> from <a href="https://github.com/react-bootstrap/react-prop-types/blob/0d1cd3a49a93e513325e3258b28a82ce7d38e690/src/deprecated.js">react-bootstrap/react-prop-types</a> as an example. The current implementation only passes down the <code>props</code>, <code>propName</code>, and <code>componentName</code> arguments:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nx">deprecated</span><span class="p">(</span><span class="nx">propType</span><span class="p">,</span> <span class="nx">explanation</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="kd">function</span> <span class="nx">validate</span><span class="p">(</span><span class="nx">props</span><span class="p">,</span> <span class="nx">propName</span><span class="p">,</span> <span class="nx">componentName</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">props</span><span class="p">[</span><span class="nx">propName</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
      <span class="kr">const</span> <span class="nx">message</span> <span class="o">=</span> <span class="err">`</span><span class="s2">&quot;${propName}&quot;</span> <span class="nx">property</span> <span class="nx">of</span> <span class="s2">&quot;${componentName}&quot;</span> <span class="nx">has</span> <span class="nx">been</span> <span class="nx">deprecated</span><span class="p">.</span><span class="err">\</span><span class="nx">n$</span><span class="p">{</span><span class="nx">explanation</span><span class="p">}</span><span class="err">`</span><span class="p">;</span>
      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">warned</span><span class="p">[</span><span class="nx">message</span><span class="p">])</span> <span class="p">{</span>
        <span class="nx">warning</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span> <span class="nx">message</span><span class="p">);</span>
        <span class="nx">warned</span><span class="p">[</span><span class="nx">message</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
      <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="nx">propType</span><span class="p">(</span><span class="nx">props</span><span class="p">,</span> <span class="nx">propName</span><span class="p">,</span> <span class="nx">componentName</span><span class="p">);</span>
  <span class="p">};</span>
<span class="p">}</span>
</code></pre></div>
<p>In order to fix the false positive, make sure you pass <strong>all</strong> arguments down to the wrapped PropType. This is easy to do with the ES6 <code>...rest</code> notation:</p>
<div class="highlight"><pre><code class="language-javascript" data-lang="javascript"><span class="kr">export</span> <span class="k">default</span> <span class="kd">function</span> <span class="nx">deprecated</span><span class="p">(</span><span class="nx">propType</span><span class="p">,</span> <span class="nx">explanation</span><span class="p">)</span> <span class="p">{</span>
  <span class="k">return</span> <span class="kd">function</span> <span class="nx">validate</span><span class="p">(</span><span class="nx">props</span><span class="p">,</span> <span class="nx">propName</span><span class="p">,</span> <span class="nx">componentName</span><span class="p">,</span> <span class="p">...</span><span class="nx">rest</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Note ...rest here</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">props</span><span class="p">[</span><span class="nx">propName</span><span class="p">]</span> <span class="o">!=</span> <span class="kc">null</span><span class="p">)</span> <span class="p">{</span>
      <span class="kr">const</span> <span class="nx">message</span> <span class="o">=</span> <span class="err">`</span><span class="s2">&quot;${propName}&quot;</span> <span class="nx">property</span> <span class="nx">of</span> <span class="s2">&quot;${componentName}&quot;</span> <span class="nx">has</span> <span class="nx">been</span> <span class="nx">deprecated</span><span class="p">.</span><span class="err">\</span><span class="nx">n$</span><span class="p">{</span><span class="nx">explanation</span><span class="p">}</span><span class="err">`</span><span class="p">;</span>
      <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">warned</span><span class="p">[</span><span class="nx">message</span><span class="p">])</span> <span class="p">{</span>
        <span class="nx">warning</span><span class="p">(</span><span class="kc">false</span><span class="p">,</span> <span class="nx">message</span><span class="p">);</span>
        <span class="nx">warned</span><span class="p">[</span><span class="nx">message</span><span class="p">]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
      <span class="p">}</span>
    <span class="p">}</span>

    <span class="k">return</span> <span class="nx">propType</span><span class="p">(</span><span class="nx">props</span><span class="p">,</span> <span class="nx">propName</span><span class="p">,</span> <span class="nx">componentName</span><span class="p">,</span> <span class="p">...</span><span class="nx">rest</span><span class="p">);</span> <span class="c1">// and here</span>
  <span class="p">};</span>
<span class="p">}</span>
</code></pre></div>
<p>This will silence the warning.</p>


    
  </div>
</section>


    <footer class="nav-footer">
  <section class="sitemap">
    <a href="/" class="nav-home">
    </a>
    <div>
      <h5><a href="/docs/">Docs</a></h5>
      <a href="/docs/hello-world.html">Quick Start</a>
      <a href="/docs/thinking-in-react.html">Thinking in React</a>
      <a href="/tutorial/tutorial.html">Tutorial</a>
      <a href="/docs/jsx-in-depth.html">Advanced Guides</a>
    </div>
    <div>
      <h5><a href="/community/support.html">Community</a></h5>
      <a href="http://stackoverflow.com/questions/tagged/reactjs" target="_blank">Stack Overflow</a>
      <a href="https://discuss.reactjs.org/" target="_blank">Discussion Forum</a>
      <a href="https://discord.gg/0ZcbPKXt5bZjGY5n" target="_blank">Reactiflux Chat</a>
      <a href="https://www.facebook.com/react" target="_blank">Facebook</a>
      <a href="https://twitter.com/reactjs" target="_blank">Twitter</a>
    </div>
    <div>
      <h5><a href="/community/support.html">Resources</a></h5>
      <a href="/community/conferences.html">Conferences</a>
      <a href="/community/videos.html">Videos</a>
      <a href="https://github.com/facebook/react/wiki/Examples" target="_blank">Examples</a>
      <a href="https://github.com/facebook/react/wiki/Complementary-Tools" target="_blank">Complementary Tools</a>
    </div>
    <div>
      <h5>More</h5>
      <a href="/blog/">Blog</a>
      <a href="https://github.com/facebook/react" target="_blank">GitHub</a>
      <a href="http://facebook.github.io/react-native/" target="_blank">React Native</a>
      <a href="/acknowledgements.html">Acknowledgements</a>
    </div>
  </section>
  <a href="https://code.facebook.com/projects/" target="_blank" class="fbOpenSource">
    <img src="/img/oss_logo.png" alt="Facebook Open Source" width="170" height="45"/>
  </a>
  <section class="copyright">
    Copyright © 2017 Facebook Inc.
  </section>
</footer>

  </div>

  <div id="fb-root"></div>
  <script src="/js/anchor-links.js"></script>
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
    ga('create', 'UA-41298772-1', 'facebook.github.io');
    ga('send', 'pageview');

    !function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="https://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");

    (function(d, s, id) {
      var js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return;
      js = d.createElement(s); js.id = id;
      js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.6&appId=623268441017527";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    docsearch({
      apiKey: '36221914cce388c46d0420343e0bb32e',
      indexName: 'react',
      inputSelector: '#algolia-doc-search'
    });
  </script>
</body>
</html>
